﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_TVCATNChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        public void LoadData()
        {
            Drp_VTYEAR.Items.Add(new ListItem((DateTime.Now.Year - 1).ToString() + "年", (DateTime.Now.Year - 1).ToString()));
            Drp_VTYEAR.Items.Add(new ListItem((DateTime.Now.Year).ToString() + "年", (DateTime.Now.Year).ToString()));
            Drp_VTYEAR.SelectedValue = DateTime.Now.Year.ToString();
            int id = 0;
            if (!string.IsNullOrEmpty(Request["tid"]))
            {
                id = int.Parse(Request["tid"]);
                HR_TVCATN _tv = TbsDbSession.Default.From<HR_TVCATN>().Where(HR_TVCATN._.ID == id).ToFirst();
                txt_EMPLID.Text = _tv.EMPLID.ToString();
                txt_TOLQTY.Text = _tv.TOLQTY.Value.ToString();
                txt_TOYEAR.Text = _tv.TOYEAR.Value.ToString("yyyy-MM-dd");
                txt_VAYEAR.Text = _tv.VAYEAR.Value.ToString("yyyy-MM-dd");
                Drp_VTYEAR.SelectedValue = _tv.VTYEAR.Value.ToString();
                txt_COMQTY.Text = _tv.COMQTY.Value.ToString();
                txt_LEFQTY.Text = _tv.LEFQTY.Value.ToString();
                
            }
            else
            {
                txt_COMQTY.Text = "0";
                txt_LEFQTY.Text = "0";
            }

            txt_COMQTY.Enabled = false;
            txt_LEFQTY.Enabled = false;

        }

        protected bool ValidateForms()
        {
            bool f = true;
            string message = "";
            if (txt_EMPLID.Text == "")
            {
                message = "请输入员工编号的信息";
                Alert.Show(message);
                txt_EMPLID.MarkInvalid(message);
                return false;
            }
            if (txt_VAYEAR.Text == "")
            {
                message = "请选择开始日期";
                Alert.Show(message);
                txt_VAYEAR.MarkInvalid(message);
                return false;
            }
            if (txt_TOYEAR.Text == "")
            {
                message = "请选择结束日期";
                Alert.Show(message);
                txt_TOYEAR.MarkInvalid(message);
                return false;
            }

            if (txt_TOLQTY.Text == "")
            {
                message = "请输入年假总天数";
                Alert.Show(message);
                txt_TOLQTY.MarkInvalid(message);
                return false;
            }

            return f;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateForms())
            {
                HR_TVCATN tv = new HR_TVCATN();
                int returnvalue = 0;
                if (!string.IsNullOrEmpty(Request["tid"]))
                {

                    tv.ID = int.Parse(Request["tid"]);
                    tv.Attach();
                    tv.EMPLID = txt_EMPLID.Text;
                    tv.TOLQTY = decimal.Parse(txt_TOLQTY.Text);
                    tv.TOYEAR = Convert.ToDateTime(txt_TOYEAR.Text);
                    tv.VAYEAR = Convert.ToDateTime(txt_VAYEAR.Text);
                    tv.VTYEAR = int.Parse(Drp_VTYEAR.SelectedValue);
                    tv.LEFQTY = tv.TOLQTY.Value - decimal.Parse(txt_COMQTY.Text);
                    returnvalue = TbsDbSession.Default.Update<HR_TVCATN>(tv);

                }
                else
                {
                    tv.DeAttach();
                    tv.EMPLID = txt_EMPLID.Text;
                    tv.TOLQTY = decimal.Parse(txt_TOLQTY.Text);
                    tv.TOYEAR = Convert.ToDateTime(txt_TOYEAR.Text);
                    tv.VAYEAR = Convert.ToDateTime(txt_VAYEAR.Text);
                    tv.VTYEAR = int.Parse(Drp_VTYEAR.SelectedValue);
                    tv.COMQTY = 0;
                    tv.LEFQTY = tv.TOLQTY.Value;
                    returnvalue = TbsDbSession.Default.Insert<HR_TVCATN>(tv);
                }
                if (returnvalue > 0)
                {
                    Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
        }

        protected void txt_TOLQTY_TextChanged(object sender, EventArgs e)
        {
            txt_LEFQTY.Text = txt_TOLQTY.Text;
        }
    }
}